﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SWBetongXaydung.GUI.Common;
using SWBetongXaydung.BLL;
using Microsoft.Reporting.WinForms;
using SWBetongXaydung.Utils;
using System.Drawing.Printing;
using QldtSdh.Utils;
using System.Threading;
namespace SWBetongXaydung.GUI.ThongKe
{
    public partial class UCThongKe_NhienLieuTieuThu : BaseUserControl
    {
        bool allowUpdate;
        private string filePathTemp = Application.StartupPath + "\\TEMP\\{0}.doc";
        private int timeToWaitExport = 4000;
        DAL.BETONGXAYDUNGEntities _dbContext = null;
        public DAL.BETONGXAYDUNGEntities DBContext
        {
            get
            {
                if (_dbContext == null)
                    _dbContext = DAL.DBContextHelper.GetContext();
                return _dbContext;
            }
        }
        WordUtil word = null;
        public UCThongKe_NhienLieuTieuThu()
        {
            InitializeComponent();
            allowUpdate = false;
            cmbThang.Text = DateTime.Now.Month.ToString();
            cmbNam.Text = DateTime.Now.Year.ToString();
            LoadXe();
            LoadTaiXe();
            allowUpdate = true;
        }

        void LoadXe()
        {
            cmbSoXe.DataSource = XeBLL.GetList();
            cmbSoXe.DisplayMember = "SoXe";
            cmbSoXe.ValueMember = "SoXe";
        }

        void LoadTaiXe()
        {
            cmbTaiXe.DataSource = TaiXeBLL.GetList();
            cmbTaiXe.DisplayMember = "TenDayDu";
            cmbTaiXe.ValueMember = "IDTaiXe";
            if (cmbSoXe.SelectedIndex != -1)
            {
                var pc = PhanCongLaiXeBLL.Get(cmbSoXe.SelectedValue.ToString());
                if (pc != null)
                {
                    if (cmbTaiXe.SelectedIndex != -1)
                    {
                        cmbTaiXe.SelectedValue = pc.IDTaiXe;
                    }
                }
            }
        }
        bool Validate_Data()
        {
            errorNotify.Clear();
            int i = 0;
            if (!int.TryParse(cmbThang.Text, out i))
            {
                errorNotify.SetError(cmbThang, "Tháng không đúng");
                return false;
            }
            if (!int.TryParse(cmbNam.Text, out i))
            {
                errorNotify.SetError(cmbNam, "Năm không đúng");
                return false;
            }
            if (cmbSoXe.SelectedIndex == -1)
            {
                errorNotify.SetError(cmbSoXe, "Bạn chưa chọn xe");
                return false;
            }
            if (cmbTaiXe.SelectedIndex == -1)
            {
                errorNotify.SetError(cmbTaiXe, "Bạn chưa chọn tài xế");
                return false;
            }

            return true;
        }
        TYPE_REPORT GetLoaiThongKe()
        {
            TYPE_REPORT loaiThongKe = TYPE_REPORT.BinhThuong;
            if (rdBinhThuong.Checked)
                loaiThongKe = TYPE_REPORT.BinhThuong;
            else if (rdCanDoi.Checked)
                loaiThongKe = TYPE_REPORT.CanDoi;
            else
                loaiThongKe = TYPE_REPORT.Thue;
            return loaiThongKe;
        }

        private void btnThongKe_Click(object sender, EventArgs e)
        {
            if (Validate_Data())
            {
                int IDTaiXe = int.Parse(cmbTaiXe.SelectedValue.ToString());
                string soXe = cmbSoXe.Text;
                int thang = int.Parse(cmbThang.Text);
                int nam = int.Parse(cmbNam.Text);
                report(IDTaiXe, soXe, thang, nam, GetLoaiThongKe());
            }
        }

        private void cmbSoXe_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (allowUpdate)
            {
                if (cmbSoXe.SelectedIndex != -1)
                {
                    var pc = PhanCongLaiXeBLL.Get(cmbSoXe.SelectedValue.ToString());
                    if (pc != null)
                    {
                        if (cmbTaiXe.SelectedIndex != -1)
                        {
                            cmbTaiXe.SelectedValue = pc.IDTaiXe;
                        }
                    }
                }
            }
        }

        private void btnThongkeTatCa_Click(object sender, EventArgs e)
        {
            if (Validate_Data())
            {
                var loaiThongKe = GetLoaiThongKe();
                int thang = int.Parse(cmbThang.Text);
                int nam = int.Parse(cmbNam.Text);
                var listTaiXe = TaiXeBLL.GetList(DBContext);
                if (listTaiXe != null && listTaiXe.Count > 0)
                    foreach (var item in listTaiXe)
                    {
                        var listDieuXe = DieuXeBLL.GetList(item.IDTaiXe);
                        if (listDieuXe != null && listDieuXe.Count > 0)
                        {
                            foreach (var dieuXe in listDieuXe)
                                report(item.IDTaiXe, dieuXe, thang, nam, loaiThongKe, false, true);
                        }
                        else
                        {
                            var pc = PhanCongLaiXeBLL.Get(item.IDTaiXe);
                            if (pc != null)
                                report(item.IDTaiXe, pc.SoXe, thang, nam, loaiThongKe, false, true);
                        }
                    }
            }
        }

        void report(int IDTaiXe, string soXe, int thang, int nam, TYPE_REPORT loaiThongKe, bool preview = true, bool printMultifile = false)
        {
            var taiXe = TaiXeBLL.GetTaiXe(IDTaiXe);
            var donGia = DonGiaNhienLieuBLL.GetDonGia(thang, nam, soXe);
            var listCapNhienLieu = ThongKeCapNhienLieu.GetList(thang, nam, IDTaiXe, soXe);
            var listNhienLieuTieuThu = ThongKeNhienLieuTieuThu.GetList(thang, nam, IDTaiXe, soXe, loaiThongKe);
            decimal tongTieuThu = (listNhienLieuTieuThu != null && listNhienLieuTieuThu.Count > 0) ? listNhienLieuTieuThu.Sum(t => t.TongLit) : 0;
            ThongKeNhienLieuTieuThuBindingSource.DataSource = listNhienLieuTieuThu;
            decimal tongCap = (listCapNhienLieu != null && listCapNhienLieu.Count > 0) ? listCapNhienLieu.Sum(t => t.LuongNhienLieu) : 0;
            switch (loaiThongKe)
            {
                case TYPE_REPORT.BinhThuong:

                    break;
                case TYPE_REPORT.CanDoi:
                    {
                        int temp = (int)tongTieuThu;
                        temp = temp / 10;
                        temp++;
                        temp = temp * 10;
                        tongCap = temp;
                    }
                    break;
                case TYPE_REPORT.Thue:
                    {
                        var listCanDoi = ThongKeNhienLieuTieuThu.GetList(thang, nam, IDTaiXe, soXe, TYPE_REPORT.CanDoi);
                        if (listCanDoi != null && listCanDoi.Count > 0)
                        {
                            int temp = (int)listCanDoi.Sum(t => t.TongLit);
                            temp = temp / 10;
                            temp++;
                            temp = temp * 10;
                            tongCap = tongCap - temp;
                        }
                    }
                    break;
            }

            decimal conLai = tongTieuThu - tongCap;
            decimal thanhTien = conLai * (donGia == null ? 0 : donGia.DonGia);
            //var tongTien = (list != null && list.Count > 0) ? list.Sum(t => t.ThanhTien) : 0;
            ReportParameter[] parameters = new ReportParameter[8];
            parameters[0] = new ReportParameter("rpThang", "THÁNG " + thang + "/" + nam);
            parameters[1] = new ReportParameter("rpTaiXe", "Tài xế: " + taiXe.TenDayDu);
            parameters[2] = new ReportParameter("rpSoXe", "Số xe: " + soXe);
            parameters[3] = new ReportParameter("rpDonGia", (donGia == null ? "0đ" : donGia.DonGia.ToString().FormatCurrency() + "đ"));
            parameters[4] = new ReportParameter("rpTongCap", ((decimal)tongCap).ToString());
            parameters[5] = new ReportParameter("rpConLai", conLai.ToString());
            parameters[6] = new ReportParameter("rpThanhTien", string.Format("{0}đ", thanhTien.ToString().FormatCurrency()));
            parameters[7] = new ReportParameter("rpTienBangChu", "Bằng chữ: " + (Math.Abs((int)Math.Floor(thanhTien))).ToString().ConvertNum2Text() + "đồng");

            System.Drawing.Printing.PageSettings pg = new System.Drawing.Printing.PageSettings();
            pg.Margins.Top = 20;
            pg.Margins.Bottom = 20;
            pg.Margins.Left = 20;
            pg.Margins.Right = 20;
            pg.Landscape = false;
            PaperSize size = new PaperSize();
            size.Width = 827;
            size.Height = 1169;
            pg.PaperSize = size;
            rpNhienLieuTieuThu.SetPageSettings(pg);
            rpNhienLieuTieuThu.LocalReport.SetParameters(parameters);
            rpNhienLieuTieuThu.RefreshReport();
            if (!preview)
            {
                rpNhienLieuTieuThu.PrinterSettings.PrinterName = PrintUtil.GetDefaultPrinter();
                string fileName = string.Format(filePathTemp, IDTaiXe);
                LocalReport report = this.rpNhienLieuTieuThu.LocalReport;
                PrintUtil.Export(report, PrintUtil.WORDFORMAT, fileName);
                Thread.Sleep(timeToWaitExport);
                word = new WordUtil(fileName);
                word.PrintDocument(true);
                if (!printMultifile)
                    word.EndWork();
            }
        }
    }
}
